<template>
  <div>
    <my-breadcrumb :items="['用户管理', '普通用户管理']"/>
    <search :formItems="searchItems" @search="getList" ref="search"/>
    <div class="add">
      <el-button type="primary" :loading="loading" @click="toExport">导出用户</el-button>
      <el-upload
        style="display: inline-block;"
        action="#"
        accept=".xls,.xlsx"
        :before-upload="beforeUpload">
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
    </div>
    <el-table :data="users" @expand-change="lazyLoad" @sort-change="orderByTime">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        label="注册时间"
        sortable="custom"
        width="110px"
        align="center"
        prop="createtime">
        <template slot-scope="scope">
          <p>{{ scope.row.createtime.split(' ')[0] }}</p>
          <p>{{ scope.row.createtime.split(' ')[1] }}</p>
        </template>
      </el-table-column>
      <el-table-column
        label="昵称"
        prop="name">
      </el-table-column>
      <el-table-column
        label="头像"
        prop="avatar">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" height="40" width="40" style="border-radius: 50%;overflow: hidden;">
        </template>
      </el-table-column>
      <el-table-column
        label="性别"
        prop="sex">
        <template slot-scope="scope">
          {{ scope.row.sex === '1' ? '男' : (scope.row.sex === '2' ? '女' : (scope.row.sex === '3' ? '未知' : '保密')) }}
        </template>
      </el-table-column>
      <el-table-column
        label="等级"
        sortable="custom"
        prop="level">
      </el-table-column>
      <el-table-column
        label="手机号"
        width="110px"
        prop="phone">
      </el-table-column>
      <el-table-column
        label="积分"
        width="110px"
        sortable="custom"
        prop="nowexperience">
      </el-table-column>
      <el-table-column
        label="裙子数量"
        sortable="custom"
        width="110px"
        prop="skirtinum">
      </el-table-column>
      <el-table-column
        label="鲜花数量"
        sortable="custom"
        width="110px"
        prop="flowercount">
      </el-table-column>
      <el-table-column
        label="地址"
        width="140px"
        prop="address">
      </el-table-column>
      <el-table-column
        label="生日"
        width="100px"
        prop="birthday">
      </el-table-column>
      <el-table-column
        label="爱好程度"
        prop="degreeid">
        <template slot-scope="scope">
          {{ degrees[scope.row.degreeid - 1] ? degrees[scope.row.degreeid - 1].name : '' }}
        </template>
      </el-table-column>
      <el-table-column
        label="是否禁用"
        prop="islogin">
        <template slot-scope="scope">
          {{ scope.row.islogin === '1' ? '否' : '已禁用' }}
        </template>
      </el-table-column>
      <el-table-column
        label="禁言状态"
        prop="prohibit">
        <template slot-scope="scope">
          {{ scope.row.prohibit === '1' ? '禁言' : '未禁言' }}
        </template>
      </el-table-column>
      <el-table-column
        label="禁言开始时间"
        width="110px"
        prop="start">
      </el-table-column>
      <el-table-column
        label="禁言结束时间"
        width="110px"
        prop="end">
      </el-table-column>
      <el-table-column
        label="禁言原因"
        prop="prohibitcontent">
      </el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        width="240px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="toSetFen(scope.row.id, scope.row.nowexperience)">修改积分</el-button>
          <el-button size="mini" type="text" @click="toDisable(scope.row.id)">{{ scope.row.islogin === '1' ? '禁用' : '启用' }}</el-button>
          <el-button size="mini" type="text" @click="toBanned(scope.row.id)" v-if="scope.row.prohibit === '0'">禁言</el-button>
          <el-button size="mini" type="text" @click="toUnbanned(scope.row.prohibitid)" v-else>解除禁言</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog title="用户信息修改" :visible.sync="showAdd">
      <el-form :model="info" label-width="70px">
        <el-form-item label="签名" prop="autograph">
          <el-input v-model="info.autograph"/>
        </el-form-item>
        <el-form-item label="点赞量" prop="fabulous">
          <el-input-number v-model="info.fabulous"/>
        </el-form-item>
        <el-form-item label="粉丝数量" prop="fans">
          <el-input-number v-model="info.fans"/>
        </el-form-item>
        <el-form-item label="关注量" prop="number">
          <el-input-number v-model="info.number"/>
        </el-form-item>
        <el-form-item label="是否禁言" prop="prohibit">
          <el-select v-model="info.prohibit">
            <el-option value="0" label="否"/>
            <el-option value="1" label="是"/>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form :model="detail" label-width="70px">
        <el-form-item label="爱好程度" prop="degreeid">
          <el-select v-model="detail.degreeid">
            <el-option v-for="(item, index) in degrees" :key="index" :value="item.id" :label="item.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="风格" prop="styleid">
          <el-select v-model="detail.styleid">
            <el-option v-for="(item, index) in styles" :key="index" :value="item.id" :label="item.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="裙子数量" prop="skirtinum">
          <el-input-number v-model="detail.skirtinum"/>
        </el-form-item>
        <el-form-item label="鲜花数量" prop="flowercount">
          <el-input-number v-model="detail.flowercount"/>
        </el-form-item>
        <el-form-item label="可赠鲜花数" prop="flower" label-width="90px">
          <el-input-number v-model="detail.flower"/>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmitUpdate">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="禁言用户" :visible.sync="showBanned" width="500px">
      <el-form :model="banned" ref="banned" label-width="70px">
        <el-form-item label="禁言时间" prop="times">
          <el-date-picker
            v-model="banned.times"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="禁言原因" prop="content">
          <el-input v-model="banned.content" type="textarea"/>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showBanned = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {toExport, deleteRow} from '../../utils/getList'
import CLAZZ from '@/config/classify'
export default {
  name: 'common',
  data () {
    return {
      searchItems: [
        {
          label: '用户名称',
          type: 'input',
          key: 'membername'
        },
        {
          label: '手机号',
          type: 'input',
          key: 'phone'
        },
        {
          label: '等级',
          type: 'input',
          key: 'level'
        },
        {
          label: '地区',
          type: 'address',
          key: 'address'
        },
        {
          label: '生日',
          type: 'times',
          key: 'times'
        },
        {
          label: '是否禁用',
          key: 'islogin',
          type: 'select',
          options: [{id: '1', name: '未禁用'}, {id: '0', name: '禁用'}]
        },
        {
          label: '是否禁言',
          key: 'prohibit',
          type: 'select',
          options: [{id: '1', name: '是'}, {id: '0', name: '否'}]
        }
      ], // 检索结构
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      users: [],
      loading: false,
      lifting: null,
      banned: {
        times: [],
        content: ''
      },
      showBanned: false,
      bannedId: '',
      degrees: CLAZZ.degreeid,
      styles: CLAZZ.style,
      showAdd: false,
      updateRow: null,
      info: {
        // phone: '',
        // password: '',
        autograph: '',
        // level: '',
        // avatar: '',
        fabulous: '',
        fans: '',
        number: '',
        prohibit: ''
      },
      detail: {
        degreeid: '',
        styleid: '',
        // address: '',
        // sex: '',
        skirtinum: '',
        flowercount: '',
        flower: ''
      }
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize, orderby: this.lifting}
      this.axios.get('/Member/index', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.users = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },

    toDelete (id) {
      deleteRow({url: '/member/delete', id}, res => {
        if (res.data.status === '1' || res.data.status === 1) {
          this.$message.success(res.data.msg)
          this.getList()
        }
      })
    },

    lazyLoad (row, expandedRows) {
    },

    toExport () {
      this.loading = true
      let searchData = this.$refs['search'].search()
      toExport('/Member/excelindex', searchData, '用户', () => {
        this.loading = false
      })
    },
    orderByTime (lifting) {
      if (lifting.order === 'ascending') {
        this.lifting = lifting.prop + ' asc'
      } else {
        this.lifting = lifting.prop + ' desc'
      }
      this.getList()
    },

    // 禁言
    toBanned (id) {
      this.bannedId = id
      this.showBanned = true
      this.$nextTick(() => {
        this.$refs['banned'].resetFields()
      })
    },
    toSubmit () {
      let params = {
        memberid: this.bannedId,
        start: this.banned.times[0],
        end: this.banned.times[1],
        content: this.banned.content
      }
      this.axios.post('/Member/prohibit', this.qs.stringify(params))
        .then(res => {
          if (res.data.status === 1) {
            this.showBanned = false
            this.$message.success(res.data.msg)
            this.getList()
          }
        })
    },

    // 禁用
    toDisable (id) {
      this.$confirm('此操作将禁用该账户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get('/Member/nologin', {params: {memberid: id}})
          .then(res => {
            if (res.data.status === 1) {
              this.$message.success(res.data.msg)
            }
            this.getList()
          })
      })
    },

    toUpdate (row) {
      this.showAdd = true
      this.updateRow = row
      this.$nextTick(() => {
        Object.keys(row).forEach(key => {
          if (this.info[key] !== undefined) {
            this.info[key] = row[key]
          }
          if (this.detail[key] !== undefined) {
            this.detail[key] = row[key]
          }
        })
      })
    },
    toSubmitUpdate () {
      let params = {
        memberinfo: this.info,
        detail: this.detail,
        id: this.updateRow.id
      }
      this.axios.post('/member/edit', this.qs.stringify(params))
        .then(res => {
          if (res.data.status === 1) {
            this.$message.success(res.data.msg)
            this.showAdd = false
            this.getList()
          }
        })
    },
    toSetFen (id, fen) {
      this.$prompt('请输入积分值', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValue: fen
      }).then(({ value }) => {
        if (Number(value) <= 1000) {
          this.axios.get('/Member/editlevel', {params: {id, nowexperience: value}})
            .then(res => {
              if (res.data.status === '1' || res.data.status === 1) {
                this.getList()
                this.$message.success('修改成功')
              }
            })
        } else {
          this.$message.warning('积分上限目前为1000')
        }
      })
    },
    toUnbanned (id) {
      this.$confirm('确认解除禁言吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get('/Member/delprohibit', {params: {prohibitid: id}})
          .then(res => {
            if (res.data.status === '1' || res.data.status === 1) {
              this.$message.success(res.data.msg)
              this.getList()
            }
          })
      })
    },

    beforeUpload (e) {
      let formData = new FormData()
      formData.append('file', e)
      this.axios.post('/Member/php_excel_import', formData, {headers: {'Content-Type': 'multipart/form-data;charset=utf-8'}})
        .then(res => {
          console.log(res)
        })
      return false
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>

</style>
